
app.controller('bindSellerCtrl', ['$scope', function ($scope) {
	
	var verifyCode = "";

    refreshSellers();
	
	/* 刷新掌柜号列表 */
	function refreshSellers() {
		AQ.callAQ("getSellers.do", null, function(result) {
			var data = [];
			for (var i = 1; i < result.length; ++i) {
				verifyCode = result[i].verifyCode;
				data.push(result[i]);
			}
			$scope.$apply(function () {
				$scope.listData = data;
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	$scope.checkSeller = function(sellerId, sellerNick, checkState) {
		if(checkState == 1) {
			Utils.msgDialog('<span style="color:#0B81DA; font-weight:bold; font-size:16px;">掌柜号“'+sellerNick +'”已经通过验证，'+
					'可以放“商家立返单”、“远程单”了。</span>');
			return;
		}
		var content = '<span style="color:#C83345; font-weight:bold; font-size:16px;">在店铺任意一个宝贝标题后面添加验证码，确认发布后复制宝贝链接进行验证</span><BR>'+
			'<span style="color:#3C3C3C; font-weight:bold;">在宝贝标题里添加验证码示例：</span>'+
			'<div style="width:700px; height:230px; border:2px solid #E4E4E4; background:url(images/seller_yz_tips.gif) no-repeat;"></div><BR>'+
			'<div style="color:#C83345; font-weight:bold; font-size:20px;">您的店铺验证码是：<input type="text" onclick="select();" readOnly="true" style="color:#3C3C3C; cursor:text; border:none;" value="'+ verifyCode +'" /></div><BR>'+
			'<span style="color:#3C3C3C; font-weight:bold; font-size:16px;">宝贝链接：</span><input type="text"class="pro-link" style="width:500px; height:35px; border:1px solid #E4E4E4; padding-left:5px;"/><BR>'+
			'<input type="button" onClick="verifyProLink('+sellerId+', this)" value="提交验证" style="width:150px; height:32px; margin:10px 0 0 80px; border:1px solid #C22538; cursor:pointer; background:#C83345; color:#fff;"/>';
		var d = dialog({
			fixed: true,
			zIndex: 999999999,
    		title: '验证店铺：'+sellerNick,
  	  		content: content,
			okValue: '返回',
    		ok: function () {}
		});
		d.width(700);
		d.height(420);
		d.showModal();
	};
	
	$scope.deleteSeller = function(sellerNick) {
		Utils.checkDialog("确定删除掌柜号“"+ sellerNick +"”吗？", "确定", "取消",
			function() {
				var param = {
					sellerNick: sellerNick,
				};
				AQ.callAQ("deleteSeller.do", param, function(result) {
					refreshSellers();
				}, function(err, code) {
					Utils.msgDialog(err + "["+ code +"]");
				});
			},
			function() {}
		);	
	};
	
	/* 绑定掌柜号 */
	$(".bd-ww-bt").click(function(e) {
		var nick = $.trim($("#wwNick").val());
		if($.trim(nick).length < 3) {
			Utils.msgDialog("请输入正确的淘宝账号！");
			return;
		}
		$(".bd-ww-bt").val("正在绑定···");
		$(".bd-ww-bt").prop("disabled", true);
		
		var param = {
			sellerNick: nick,
		};
        AQ.callAQ("bindSeller.do", param, function(result) {
			$(".bd-ww-bt").val("绑定");
			$(".bd-ww-bt").prop("disabled", false);
			Utils.msgDialog(result[0].info);
			refreshSellers();
		}, function(err, code) {
			$(".bd-ww-bt").val("绑定");
			$(".bd-ww-bt").prop("disabled", false);
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
}]);


/**
 * 提交验证
 */
function verifyProLink(sellerId, thisDom) {
	var proLink = $.trim($(thisDom).siblings(".pro-link").val());
	if(!(proLink.indexOf("https://item.taobao.com/item.htm?") >= 0 || proLink.indexOf("https://detail.tmall.com/item.htm?") >= 0)) {
		Utils.msgDialog("请输入正确的淘宝、天猫宝贝链接！");
		return;
	}
	
	$(thisDom).val("正在验证···");
	$(thisDom).prop("disabled", true);
	
	var param = {
			sellerId: sellerId,
			productLink: proLink,
		};
        AQ.callAQ("verifySeller.do", param, function(result) {
        	$(".ui-dialog").remove();
			$(".ui-popup-backdrop").remove();
			alert("验证通过！（请删除宝贝标题中的验证码）");
		}, function(err, code) {
			$(thisDom).val("提交验证");
			$(thisDom).prop("disabled", false);
			Utils.msgDialog(err);
		});
}
